//
//  HandBackHandTrainVC.m
//  CoolTennisBall
//
//  Created by Coollang on 16/7/19.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "HandBackHandTrainVC.h"
#import "ArcView.h"
#import "ChartItem.h"
#import "Masonry.h"
#import "UIView+Frame.h"
#import <UICountingLabel/UICountingLabel.h>
#import "CustomProgressBarView.h"
#import "RealTimeTrainModel.h"
#import "NSString+Extension.h"

static const NSUInteger kMaxStrength = 30;
static const NSUInteger kMaxSpeed = 180;
static const NSUInteger kMaxAngle = 360;
static const CGFloat kTimeInterval = 1.0f;

@interface HandBackHandTrainVC ()
//实时动作显示的图片
@property (weak, nonatomic) IBOutlet UIImageView *actionImageView;
@property (weak, nonatomic) IBOutlet UILabel *actionNameLabel;

@property (weak, nonatomic) IBOutlet ArcView *angleArcview;
@property (weak, nonatomic) IBOutlet ArcView *speedArcview;
@property (weak, nonatomic) IBOutlet ArcView *strengthArcview;
@property (weak, nonatomic) IBOutlet UIImageView *angleArcBgImageView;
@property (weak, nonatomic) IBOutlet UIImageView *speedArcBgImageView;
@property (weak, nonatomic) IBOutlet UIImageView *strengthArcBbImageView;

/** 正反手柱状图标 */
/** 左边 */
@property (weak, nonatomic) IBOutlet UILabel *leftHandLabel;
@property (weak, nonatomic) IBOutlet UILabel *leftKmLabel;
@property (weak, nonatomic) IBOutlet UILabel *leftSpeedLabel;
@property (weak, nonatomic) IBOutlet UILabel *LShangxuanNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *LPingJiNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *LXueQiuNumLabel;


@property (weak, nonatomic) IBOutlet CustomProgressBarView *LShangXuanProgressView;
@property (weak, nonatomic) IBOutlet CustomProgressBarView *LPingProgressView;
@property (weak, nonatomic) IBOutlet CustomProgressBarView *LXueQiuProgressView;

/** 右边 */
@property (weak, nonatomic) IBOutlet UILabel *RHandLabel;
@property (weak, nonatomic) IBOutlet UILabel *RKmLabel;
@property (weak, nonatomic) IBOutlet UILabel *RSpeedLabel;
@property (weak, nonatomic) IBOutlet UILabel *RShangxuanNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *RPingJiNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *RXueQiuNumLabel;

@property (weak, nonatomic) IBOutlet CustomProgressBarView *RShangXuanProgressView;
@property (weak, nonatomic) IBOutlet CustomProgressBarView *RPingProgressView;
@property (weak, nonatomic) IBOutlet CustomProgressBarView *RXueQiuProgressView;


/** 底部时间栏 */
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UILabel *hitBallFrequencyLabel;
@property (nonatomic, strong) UIView *lineView;

/** 正手颜色 */
@property (nonatomic, strong) UIColor *handColor;
/** 反手颜色 */
@property (nonatomic, strong) UIColor *backHandColor;

@property (nonatomic, strong) NSMutableArray *chartItemArr;
/** 正手球个数 */
@property (nonatomic, assign)NSInteger frontHandShangXuanCount;
@property (nonatomic, assign)NSInteger frontHandPingJiCount;
@property (nonatomic, assign)NSInteger frontHandXueQiuCount;

/** 反手球个数 */
@property (nonatomic, assign)NSInteger backHandShangXuanCount;
@property (nonatomic, assign)NSInteger backHandPingJiCount;
@property (nonatomic, assign)NSInteger backHandXueQiuCount;


/**  国际化 */
@property (weak, nonatomic) IBOutlet UILabel *swingArcLab;
@property (weak, nonatomic) IBOutlet UILabel *swingSpeedLab;
@property (weak, nonatomic) IBOutlet UILabel *swingStrongLab;


@property (weak, nonatomic) IBOutlet UILabel *LAvgLab;

@property (weak, nonatomic) IBOutlet UILabel *LShangxuanLab;
@property (weak, nonatomic) IBOutlet UILabel *LPingjiLab;
@property (weak, nonatomic) IBOutlet UILabel *LxueqiuLab;

@property (weak, nonatomic) IBOutlet UILabel *RAvgLab;

@property (weak, nonatomic) IBOutlet UILabel *RShangxuanLab;
@property (weak, nonatomic) IBOutlet UILabel *RPingjiLab;
@property (weak, nonatomic) IBOutlet UILabel *RxueqiuLab;

@end

@implementation HandBackHandTrainVC {
    
    BOOL _endTrain;
    __block CGFloat forehandAverageSpeed;
    __block CGFloat backhandAverageSpeed;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configureLocalization];
    [self configureArcViews];
    [self setUpUIOfHandBackHand];
    
    
    forehandAverageSpeed = 0;
    backhandAverageSpeed = 0;
    
    _endTrain = YES; // 默认没有开始
    
}


- (void)endTrain:(BOOL)isend {
    
    _endTrain = isend;
    
    if (isend) {
        self.frontHandXueQiuCount = 0;
        self.frontHandPingJiCount = 0;
        self.frontHandShangXuanCount = 0;
        self.backHandXueQiuCount = 0;
        self.backHandPingJiCount = 0;
        self.backHandShangXuanCount = 0;
        [self.chartItemArr removeAllObjects];
        [self calculateAverageSpeed];
        self.timeLabel.text = [NSString stringWithTrainTime:0];
    }else {
        [self addAnimtaion];
    }
    
}


- (void)setChartItem:(ChartItem *)chartItem {
    _chartItem = chartItem;
    
    if (!_endTrain) {
        
        [self.chartItemArr addObject:chartItem];
        [self calculateAverageSpeed]; // 设置平均速度
        //计算击球频率
        NSInteger frequency = 60 * (self.chartItemArr.count - 1) / self.trainTime;
        
        if ([NSBundle currentLanguageIsChinese]) {
           self.hitBallFrequencyLabel.text = [NSString stringWithFormat:@"击球频率:%zd次/分钟",frequency];
        }else {
           self.hitBallFrequencyLabel.text = [NSString stringWithFormat:@"Frequency:%zd times/min",frequency];
        }
        [self setFrequencyAndPrivateCompleteWith:chartItem];
    }
    [self configureForViewWithItem:chartItem];
    
}

// 计算平均速度
- (void)calculateAverageSpeed {
    
    __block CGFloat forehandTotalSpeed = 0;
    __block CGFloat backhandTotalSpeed = 0;
    __block CGFloat forehandCount = 0;
    __block CGFloat backhandCount = 0;
    
    [self.chartItemArr enumerateObjectsUsingBlock:^(ChartItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if (obj.handDirectionType == HandDirectionTypeForeward) {
            forehandTotalSpeed += obj.maxSpeed.floatValue;
            forehandCount++;
        }else {
            backhandTotalSpeed += obj.maxSpeed.floatValue;
            backhandCount++;
        }

    }];
    //0:左手，1:右手
    forehandAverageSpeed = forehandTotalSpeed / forehandCount;
    backhandAverageSpeed = backhandTotalSpeed / backhandCount;
    
    if (forehandCount <= 0) {
        [User getLastUser].userInforItem.hand == 1 ? (self.leftSpeedLabel.text = @"0"):(self.RSpeedLabel.text = @"0");
    }else {
        [User getLastUser].userInforItem.hand == 1 ? (self.leftSpeedLabel.text = [NSString stringWithFormat:@"%0.f",forehandAverageSpeed]):(self.RSpeedLabel.text = [NSString stringWithFormat:@"%0.f",forehandAverageSpeed]);
    }
    
    if (backhandCount <= 0) {
        [User getLastUser].userInforItem.hand == 1 ? (self.RSpeedLabel.text = @"0"):(self.leftSpeedLabel.text = @"0");
    }else {
        [User getLastUser].userInforItem.hand == 1 ? (self.RSpeedLabel.text = [NSString stringWithFormat:@"%0.f",backhandAverageSpeed]):(self.leftSpeedLabel.text = [NSString stringWithFormat:@"%0.f",backhandAverageSpeed]);
    }
  
}


// 计算击球频率和正反手完成度
- (void)setFrequencyAndPrivateCompleteWith:(ChartItem *)item {
    
    switch (item.poseType) {
        case SportPoseTypeXuanQiu:
           
            if (item.handDirectionType == HandDirectionTypeForeward) {
                
                if (item.maxSpeed.floatValue >= [self.targetModel.frontHandPaiSuTargetSpeed floatValue]) {
                    self.frontHandShangXuanCount++;
                }
            }else {
                if (item.maxSpeed.floatValue >= [self.targetModel.backHandPaiSuTargetSpeed floatValue]) {
                    self.backHandShangXuanCount++;
                }
            }
           // item.handDirectionType == HandDirectionTypeForeward ? self.frontHandShangXuanCount++ : self.backHandShangXuanCount++;
            break;
        case SportPoseTypePingJi:
            if (item.handDirectionType == HandDirectionTypeForeward) {
                
                if (item.maxSpeed.floatValue >= [self.targetModel.frontHandPaiSuTargetSpeed floatValue]) {
                    self.frontHandPingJiCount++;
                }
            }else {
                if (item.maxSpeed.floatValue >= [self.targetModel.backHandPaiSuTargetSpeed floatValue]) {
                    self.backHandPingJiCount++;
                }
            }
            // item.handDirectionType == HandDirectionTypeForeward ? self.frontHandPingJiCount++ : self.backHandPingJiCount++;
            break;
        case SportPoseTypeXueQiu:
            if (item.handDirectionType == HandDirectionTypeForeward) {
                
                if (item.maxSpeed.floatValue >= [self.targetModel.frontHandPaiSuTargetSpeed floatValue]) {
                    self.frontHandXueQiuCount++;
                }
            }else {
                if (item.maxSpeed.floatValue >= [self.targetModel.backHandPaiSuTargetSpeed floatValue]) {
                    self.backHandXueQiuCount++;
                }
            }
            // item.handDirectionType == HandDirectionTypeForeward ? self.frontHandXueQiuCount++ : self.backHandXueQiuCount++;
            break;
            
        default:
            break;
    }
}

// 创建时间线
- (void)creatTimeLine {
    if (self.lineView == nil) {
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = [UIColor colorWithHex:0x8f8f8f alpha:1.0];
        [self.bottomView addSubview:lineView];
        self.lineView = lineView;
        self.lineView.frame = CGRectMake(kYFScreenWidth, 50 , kYFScreenWidth / 20,1);
    }
}



- (void)configureLocalization {
    // 添加时间线
    [self creatTimeLine];
    self.actionImageView.image = [UIImage imageNamed:@"realTime_icon_font_xuanqiu"];
    
    self.swingSpeedLab.text = NSLocalizedString(@"RealTime.SwingSpeed", nil);
    self.swingArcLab.text = NSLocalizedString(@"Radian", nil);
    self.swingStrongLab.text = NSLocalizedString(@"Strength", nil);
    self.LAvgLab.text = NSLocalizedString(@"RealTime.Avg", nil);
    
    self.LShangxuanLab.text = NSLocalizedString(@"Topspin", nil);
    self.LPingjiLab.text = NSLocalizedString(@"Flat", nil);
    self.LxueqiuLab.text = NSLocalizedString(@"Slice", nil);
    self.RAvgLab.text = NSLocalizedString(@"RealTime.Avg", nil);
    
    self.RShangxuanLab.text = NSLocalizedString(@"Topspin", nil);
    self.RPingjiLab.text = NSLocalizedString(@"Flat", nil);
    self.RxueqiuLab.text = NSLocalizedString(@"Slice", nil);
    
    if ([NSBundle currentLanguageIsChinese]) {
        self.hitBallFrequencyLabel.text = [NSString stringWithFormat:@"击球频率:0次/分钟"];
        self.RHandLabel.font = [UIFont fontWithDescriptor:FontDescriptorMedium(14) size:0.0];
        self.leftHandLabel.font = [UIFont fontWithDescriptor:FontDescriptorMedium(14) size:0.0];
        self.RKmLabel.font = [UIFont fontWithDescriptor:FontDescriptorMedium(13) size:0.0];
        self.leftKmLabel.font = [UIFont fontWithDescriptor:FontDescriptorMedium(13) size:0.0];
         self.RSpeedLabel.font =  [UIFont fontWithDescriptor:FontDescriptorMedium(16) size:0.0];
         self.leftSpeedLabel.font =  [UIFont fontWithDescriptor:FontDescriptorMedium(14) size:0.0];
        
    }else {
        self.hitBallFrequencyLabel.text = [NSString stringWithFormat:@"%@:0 %@/min",NSLocalizedString(@"Frequency",nil),NSLocalizedString(@"times", nil)];
        self.RAvgLab.font = [UIFont fontWithDescriptor:FontDescriptorMedium(11) size:0.0];
        self.LAvgLab.font = [UIFont fontWithDescriptor:FontDescriptorMedium(11) size:0.0];
        
        self.RHandLabel.font = [UIFont fontWithDescriptor:FontDescriptorMedium(12) size:0.0];
        self.leftHandLabel.font = [UIFont fontWithDescriptor:FontDescriptorMedium(12) size:0.0];
        self.RKmLabel.font = [UIFont fontWithDescriptor:FontDescriptorMedium(10) size:0.0];
        self.leftKmLabel.font = [UIFont fontWithDescriptor:FontDescriptorMedium(10) size:0.0];
        self.RSpeedLabel.font =  [UIFont fontWithDescriptor:FontDescriptorMedium(14) size:0.0];
         self.leftSpeedLabel.font =  [UIFont fontWithDescriptor:FontDescriptorMedium(14) size:0.0];
    }
    
    NSString *sportTypeString = NSLocalizedString(@"Topspin", nil);
    self.actionNameLabel.text = [NSString stringWithFormat:@"%@ %@", [self descriptionWithHandDirectionType:HandDirectionTypeForeward],sportTypeString];
    self.actionNameLabel.textColor = kOrangeColor;

}

#pragma mark - 配置动作图片和标题
// 根据正反手来设置文字和颜色
- (void)setUpUIOfHandBackHand {
    
    // 正手为橙色， 反手为绿色
    if ([User getLastUser].userInforItem.hand) {//0:左手，1:右手
        // 左为正手
        self.LShangXuanProgressView.colors = @[[UIColor colorWithHex:0xf78608],[UIColor colorWithHex:0xffc730]];
        self.LPingProgressView.colors = @[[UIColor colorWithHex:0xf78608],[UIColor colorWithHex:0xffc730]];
        self.LXueQiuProgressView.colors = @[[UIColor colorWithHex:0xf78608],[UIColor colorWithHex:0xffc730]];
        self.leftKmLabel.textColor = kOrangeColor;
        self.leftSpeedLabel.textColor = kOrangeColor;
        
        self.RShangXuanProgressView.colors =  @[[UIColor colorWithHex:0x25c9ad], [UIColor colorWithHex:0x83f09f]];
        self.RPingProgressView.colors =  @[[UIColor colorWithHex:0x25c9ad], [UIColor colorWithHex:0x83f09f]];
        self.RXueQiuProgressView.colors = @[[UIColor colorWithHex:0x25c9ad], [UIColor colorWithHex:0x83f09f]];
        
        self.RKmLabel.textColor = kSelectedColor;
        self.RSpeedLabel.textColor = kSelectedColor;
        
        self.leftHandLabel.text = NSLocalizedString(@"RealTimeForehand", nil);
        self.RHandLabel.text = NSLocalizedString(@"RealTimeBackhand", nil);
        
    }else {
        // 右为正手
        self.RShangXuanProgressView.colors = @[[UIColor colorWithHex:0xf78608],[UIColor colorWithHex:0xffc730]];
        self.RPingProgressView.colors = @[[UIColor colorWithHex:0xf78608],[UIColor colorWithHex:0xffc730]];
        self.RXueQiuProgressView.colors = @[[UIColor colorWithHex:0xf78608],[UIColor colorWithHex:0xffc730]];
        self.RKmLabel.textColor = kOrangeColor;
        self.RSpeedLabel.textColor = kOrangeColor;
        
        self.LShangXuanProgressView.colors =  @[[UIColor colorWithHex:0x25c9ad], [UIColor colorWithHex:0x83f09f]];
        self.LPingProgressView.colors =  @[[UIColor colorWithHex:0x25c9ad], [UIColor colorWithHex:0x83f09f]];
        self.LXueQiuProgressView.colors =  @[[UIColor colorWithHex:0x25c9ad], [UIColor colorWithHex:0x83f09f]];
        self.leftKmLabel.textColor = kSelectedColor;
        self.leftSpeedLabel.textColor = kSelectedColor;
        self.RHandLabel.text = NSLocalizedString(@"RealTimeForehand", nil);
        self.leftHandLabel.text = NSLocalizedString(@"RealTimeBackhand", nil);
        

    }
    self.handColor = kOrangeColor;
    self.backHandColor = kSelectedColor;
    
}
// 设置动作图片和动作标题
- (void)configureForViewWithItem:(ChartItem *)item
{
    if (!item.hasHitTheBall) {
        //        [self showHitEmpty];
        return;
    }
    [self creatDotView:item];
    
    NSInteger swingSpeed = [item.maxSpeed integerValue];
    
    swingSpeed > kMaxSpeed ? (swingSpeed = kMaxSpeed) : (swingSpeed = swingSpeed);
    
    [self.speedArcview updateChartByCurrent:@(swingSpeed)];
    [self.strengthArcview updateChartByCurrent:@([item.strength integerValue]*11/100)];
    [self.angleArcview updateChartByCurrent:@([item.arc integerValue])];
    
    
    NSString *sportTypeString = nil;
    NSString *topTitle = nil;
    switch (item.poseType) {
        case SportPoseTypeXuanQiu:{
            self.actionImageView.image = item.handDirectionType ? [UIImage imageNamed:@"realTime_icon_font_xuanqiu"]:[UIImage imageNamed:@"realTime_icon_back_xuanqiu"];
            
            sportTypeString = NSLocalizedString(@"Topspin", nil);
            
            topTitle = [NSString stringWithFormat:@"%@ %@", [self descriptionWithHandDirectionType:item.handDirectionType],sportTypeString];
        }
            break;
        case SportPoseTypePingJi:{
            
            self.actionImageView.image = item.handDirectionType ? [UIImage imageNamed:@"realTime_icon_font_Pingji"]:[UIImage imageNamed:@"realTime_icon_back_Pingji"];
            
            sportTypeString = NSLocalizedString(@"Flat", nil);
            
            topTitle = [NSString stringWithFormat:@"%@ %@", [self descriptionWithHandDirectionType:item.handDirectionType],sportTypeString];
        }
            break;
        case SportPoseTypeXueQiu:{
            
            self.actionImageView.image = item.handDirectionType ? [UIImage imageNamed:@"realTime_icon_font_xueqiu"]:[UIImage imageNamed:@"realTime_icon_back_xueqiu"];
            
            sportTypeString = NSLocalizedString(@"Slice", nil);
            topTitle = [NSString stringWithFormat:@"%@ %@", [self descriptionWithHandDirectionType:item.handDirectionType],sportTypeString];
        }
            break;
        case SportPoseTypeJieJi:{
            
            self.actionImageView.image = item.handDirectionType ? [UIImage imageNamed:@"realTime_icon_font_Jieji"]:[UIImage imageNamed:@"realTime_icon_back_Jieji"];
            
            sportTypeString = NSLocalizedString(@"Volley", nil);
            topTitle = [NSString stringWithFormat:@"%@", sportTypeString];
        }
            break;
        case SportPoseTypeKouSha:{
            
            self.actionImageView.image = [UIImage imageNamed:@"realTime_icon_gaoya"];
            
            sportTypeString = NSLocalizedString(@"Smash", nil);
            topTitle = [NSString stringWithFormat:@"%@", sportTypeString];
        }
            break;
        case SportPoseTypeFaQiu:{
            self.actionImageView.image = [UIImage imageNamed:@"realTime_icon_faqiu"];
            
            sportTypeString = NSLocalizedString(@"Serve", nil);
            topTitle = [NSString stringWithFormat:@"%@",sportTypeString];
        }
            break;
            
        default:
            break;
    }
    
    self.actionNameLabel.text = topTitle;
    
}
- (void)configureArcViews
{
    self.strengthArcview.total = @(kMaxStrength);
    self.speedArcview.total = @(kMaxSpeed);
    self.angleArcview.total = @(kMaxAngle);
    
    self.strengthArcview.duration = kTimeInterval;
    self.speedArcview.duration = kTimeInterval;
    self.angleArcview.duration = kTimeInterval;
    
    self.strengthArcview.countingLabel.font = [UIFont systemFontOfSize:20];
    self.speedArcview.countingLabel.font = [UIFont systemFontOfSize:20];
    self.angleArcview.countingLabel.font = [UIFont systemFontOfSize:20];
    self.strengthArcview.countingLabel.textColor = kHasReadColor;
    self.speedArcview.countingLabel.textColor = kHasReadColor;
    self.angleArcview.countingLabel.textColor = kHasReadColor;
    
    [self.strengthArcview strokeChart];
    [self.speedArcview strokeChart];
    [self.angleArcview strokeChart];
}

- (NSString *)descriptionWithHandDirectionType:(HandDirectionType)handDirectionType {
    
    if (handDirectionType == HandDirectionTypeForeward) {
        self.actionNameLabel.textColor = self.handColor;
        return NSLocalizedString(@"Forehand", nil);
    } else {
        self.actionNameLabel.textColor = self.backHandColor;
        return NSLocalizedString(@"Backhand", nil);
    }
}

- (void)setTargetModel:(RealTimeTrainModel *)targetModel {
    _targetModel = targetModel;
    // 立即刷新进度条
    [self updateCompleteprogressBar];
}
// 立即刷新进度条
- (void)updateCompleteprogressBar {
    self.frontHandShangXuanCount = self.frontHandShangXuanCount;
    self.frontHandPingJiCount = self.frontHandPingJiCount;
    self.frontHandXueQiuCount = self.frontHandXueQiuCount;
    
    self.backHandShangXuanCount = self.backHandShangXuanCount;
    self.backHandPingJiCount = self.backHandPingJiCount;
    self.backHandXueQiuCount = self.backHandXueQiuCount;
}

- (void)setFrontHandShangXuanCount:(NSInteger)frontHandShangXuanCount {
    _frontHandShangXuanCount = frontHandShangXuanCount;
    
   CGFloat value = (CGFloat)frontHandShangXuanCount / [self.targetModel.frontHandShangXuanTarget floatValue];
    
    if([User getLastUser].userInforItem.hand){
        
        NSString *string = @(frontHandShangXuanCount).stringValue;
        
        if ((CGFloat)frontHandShangXuanCount >= [self.targetModel.frontHandShangXuanTarget floatValue]) {
            
            string = NSLocalizedString(@"RealTime.Complete", nil);;
        }
        
        self.LShangxuanNumLabel.text = string;
        [self.LShangXuanProgressView updateProgroess:value];
    }else {
        NSString *string = @(frontHandShangXuanCount).stringValue;
        if ((CGFloat)frontHandShangXuanCount >= [self.targetModel.frontHandShangXuanTarget floatValue]) {
            string = NSLocalizedString(@"RealTime.Complete", nil);;
        }
        
        self.RShangxuanNumLabel.text = string;
        [self.RShangXuanProgressView updateProgroess:value];
    }
}
- (void)setFrontHandPingJiCount:(NSInteger)frontHandPingJiCount {
    _frontHandPingJiCount = frontHandPingJiCount;
    CGFloat value = (CGFloat)frontHandPingJiCount / [self.targetModel.frontHandPingJiTarget floatValue];
    
    if([User getLastUser].userInforItem.hand){
        
        NSString *string = @(frontHandPingJiCount).stringValue;

        if ((CGFloat)frontHandPingJiCount >= [self.targetModel.frontHandPingJiTarget floatValue]) {
            string = NSLocalizedString(@"RealTime.Complete", nil);;
        }
        
        self.LPingJiNumLabel.text = string;
        [self.LPingProgressView updateProgroess:value];
        
    }else {
        NSString *string = @(frontHandPingJiCount).stringValue;
        
        if ((CGFloat)frontHandPingJiCount >= [self.targetModel.frontHandPingJiTarget floatValue]) {
            string = NSLocalizedString(@"RealTime.Complete", nil);;
        }
        
        self.RPingJiNumLabel.text = string;
        [self.RPingProgressView updateProgroess:value];
    }
}
- (void)setFrontHandXueQiuCount:(NSInteger)frontHandXueQiuCount {
    _frontHandXueQiuCount = frontHandXueQiuCount;
    CGFloat value = (CGFloat)frontHandXueQiuCount / [self.targetModel.frontHandXueQiuTarget floatValue];
    
    if([User getLastUser].userInforItem.hand){
        NSString *string = @(frontHandXueQiuCount).stringValue;

        if ((CGFloat)frontHandXueQiuCount >= [self.targetModel.frontHandXueQiuTarget floatValue]) {
            string = NSLocalizedString(@"RealTime.Complete", nil);;
        }
        self.LXueQiuNumLabel.text = string;
        [self.LXueQiuProgressView updateProgroess:value];
    }else {
        NSString *string = @(frontHandXueQiuCount).stringValue;
        
        if ((CGFloat)frontHandXueQiuCount >= [self.targetModel.frontHandXueQiuTarget floatValue]) {
            string = NSLocalizedString(@"RealTime.Complete", nil);;
        }
        
        self.RXueQiuNumLabel.text = string;
        [self.RXueQiuProgressView updateProgroess:value];
    }
}

- (void)setBackHandShangXuanCount:(NSInteger)backHandShangXuanCount {
    _backHandShangXuanCount = backHandShangXuanCount;
    
    CGFloat value = (CGFloat)backHandShangXuanCount / [self.targetModel.backHandShangXuanTarget floatValue];
    
    if([User getLastUser].userInforItem.hand){
        
        NSString *string = @(backHandShangXuanCount).stringValue;

        if ((CGFloat)backHandShangXuanCount >= [self.targetModel.backHandShangXuanTarget floatValue]) {
            string = NSLocalizedString(@"RealTime.Complete", nil);;
        }
        self.RShangxuanNumLabel.text = string;
        [self.RShangXuanProgressView updateProgroess:value];
    }else {
        NSString *string = @(backHandShangXuanCount).stringValue;
        
        if ((CGFloat)backHandShangXuanCount >= [self.targetModel.backHandShangXuanTarget floatValue]) {
            string = NSLocalizedString(@"RealTime.Complete", nil);;
        }
        self.LShangxuanNumLabel.text = string;
        [self.LShangXuanProgressView updateProgroess:value];
    }
}

- (void)setBackHandPingJiCount:(NSInteger)backHandPingJiCount {
    _backHandPingJiCount = backHandPingJiCount;
    
    CGFloat value = (CGFloat)backHandPingJiCount / [self.targetModel.backHandPingJiTarget floatValue];
    
    if([User getLastUser].userInforItem.hand){
        
        NSString *string = @(backHandPingJiCount).stringValue;
        if ((CGFloat)backHandPingJiCount >= [self.targetModel.backHandPingJiTarget floatValue]) {
            string = NSLocalizedString(@"RealTime.Complete", nil);;
        }
        self.RPingJiNumLabel.text = string;
        [self.RPingProgressView updateProgroess:value];

    }else {
        NSString *string = @(backHandPingJiCount).stringValue;
        if ((CGFloat)backHandPingJiCount >= [self.targetModel.backHandPingJiTarget floatValue]) {
            string = NSLocalizedString(@"RealTime.Complete", nil);;
        }
        self.LPingJiNumLabel.text = string;
        [self.LPingProgressView updateProgroess:value];
        
    }
    
}

- (void)setBackHandXueQiuCount:(NSInteger)backHandXueQiuCount {
    _backHandXueQiuCount = backHandXueQiuCount;
    CGFloat value = (CGFloat)backHandXueQiuCount / [self.targetModel.backHandXueQiuTarget floatValue];
    
    if([User getLastUser].userInforItem.hand){
        NSString *string = @(backHandXueQiuCount).stringValue;
        
        if ((CGFloat)backHandXueQiuCount > [self.targetModel.backHandXueQiuTarget floatValue]){
            string = NSLocalizedString(@"RealTime.Complete", nil);;
        }
        self.RXueQiuNumLabel.text = string;
        [self.RXueQiuProgressView updateProgroess:value];
        
    }else {
        NSString *string = @(backHandXueQiuCount).stringValue;
        if ((CGFloat)backHandXueQiuCount > [self.targetModel.backHandXueQiuTarget floatValue]){
            string = NSLocalizedString(@"RealTime.Complete", nil);;
        }
        self.LXueQiuNumLabel.text = string;
        [self.LXueQiuProgressView updateProgroess:value];

    }
}

#pragma mark - 添加时间线动画
- (void)addAnimtaion {
    
    __weak typeof(self) weakSelf = self;
    
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        weakSelf.lineView.transform = CGAffineTransformTranslate(self.lineView.transform , - kYFScreenWidth / 20, 0);
    } completion:^(BOOL finished) {
        if (_endTrain) {
            [weakSelf.lineView removeFromSuperview];
            weakSelf.lineView = nil;
            [weakSelf creatTimeLine];
            
        }else {
            weakSelf.lineView.width += kYFScreenWidth / 20;
            [weakSelf addAnimtaion];
        }
    }];
    
}
- (void)creatDotView:(ChartItem *)chartItem {
    if (!_endTrain) {
        // 是这三种球才在时间线上显示
        if (chartItem.poseType == SportPoseTypeXuanQiu || chartItem.poseType == SportPoseTypeXueQiu || chartItem.poseType == SportPoseTypePingJi) {
            
            CALayer *dotLayer = [CALayer layer];
            dotLayer.bounds = CGRectMake(0, 0, 14, 14);
            dotLayer.cornerRadius = 7;
            dotLayer.masksToBounds = YES;
            
            UIColor *backGroundColor = kUnSelectedColor;
            
            switch (chartItem.handDirectionType) {
                case HandDirectionTypeForeward:
                    if ([chartItem.maxSpeed floatValue] >= [self.targetModel.frontHandPaiSuTargetSpeed floatValue]) {
                        
                        backGroundColor = kOrangeColor;
                    }
                
                    break;
                case HandDirectionTypeBackward:
                    if ([chartItem.maxSpeed floatValue] >= [self.targetModel.backHandPaiSuTargetSpeed floatValue]) {
                        backGroundColor = kSelectedColor;
                    }
                    break;
                    
                default:
                    break;
            }

            dotLayer.backgroundColor = backGroundColor.CGColor;
            dotLayer.position = CGPointMake(self.lineView.bounds.size.width , 0);
            [self.lineView.layer addSublayer:dotLayer];
        }
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)chartItemArr {
    if (_chartItemArr == nil) {
        _chartItemArr = [NSMutableArray array];
    }
    return _chartItemArr;
}

- (void)dealloc {
    NSLog(@"HandBackHandTraniVc");
}
@end


